package com.gin.kotlin.no_gay_alliance.adapter.me

import android.content.Context
import android.view.View
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.ViewUtils.Companion.visible
import com.gin.kotlin.no_gay_alliance.databinding.ItemMissionBinding
import com.gin.kotlin.no_gay_alliance.model.MissionInfoModel
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.MissionInfoProvider
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import com.gin.nga.response.mission.MissionCheckBody
import retrofit2.Call
import retrofit2.Response
import java.time.ZonedDateTime

class MissionAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemMissionBinding, MissionInfoModel>(context, handler), View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemMissionBinding>, position: Int, b: ItemMissionBinding, dataModel: MissionInfoModel) {
        b.tvName.text = dataModel.name
        b.tvName.visibility = visible(dataModel.name)

        b.tvInfo.text = dataModel.info
        b.tvInfo.visibility = visible(dataModel.info)

        b.tvDetail.text = dataModel.detail
        b.tvDetail.visibility = visible(dataModel.detail)

        b.tvPeriodOfValidity.visibility = visible(dataModel.startTime ?: dataModel.endTime)
        val start = format(dataModel.startTime)
        val end = format(dataModel.endTime)
        b.tvPeriodOfValidity.text = "有效期: $start ~ $end"

        b.tvCount.text = "连续完成 ${dataModel.count} 次，总共完成 ${dataModel.total} 次"

        b.tvCompletedTime.text = format(dataModel.completedTime)?.let { "完成时间: $it" }
        b.tvCompletedTime.visibility = visible(dataModel.completedTime)

        b.tvCompletedTimeLast.text = format(dataModel.lastCountTime)?.let { "上次完成时间: $it" }
        b.tvCompletedTimeLast.visibility = visible(dataModel.lastCountTime)

        val time =dataModel.lastCountTime?: dataModel.completedTime
        b.tvTagCompleted.visibility = if (time!=null && dataModel.thisCountTime!=null && time.isAfter(dataModel.thisCountTime)) View.VISIBLE else View.GONE

        b.root.tag = dataModel
        b.root.setOnClickListener(this)

    }

    companion object {
        fun format(zdt: ZonedDateTime?) = zdt?.let { TimeUtils.SIMPLE_DATE_TIME_FORMATTER.format(it) }
    }

    override fun onClick(v: View?) {
        val model = v?.tag as? MissionInfoModel ?: return
        val index = data.indexOf(model)

        // 手动完成任务
        App.INSTANCE.ngaApi.missionApi.check(model.id!!).enqueue(object : NgaCallback<MissionCheckBody> {
            override fun onSuccess(
                call: Call<NgaResponse<MissionCheckBody>>,
                response: Response<NgaResponse<MissionCheckBody>>,
                body: NgaResponse<MissionCheckBody>?,
                data: MissionCheckBody?
            ) {
                data?.message?.also { handler.toast(it) } ?:handler.toast("任务已完成")
                data?.missionInfo?.also { replaceData(index, MissionInfoModel(it)) }
                MissionInfoProvider.getInstance().clear()
            }

            override fun onError(
                call: Call<NgaResponse<MissionCheckBody>>,
                response: Response<NgaResponse<MissionCheckBody>>,
                body: NgaResponse<MissionCheckBody>?,
                errorMessage: String
            ) {
                handler.toastLong(errorMessage)
            }
        })
    }
}